#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e3+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<int>g[maxn];
vector<int>pp;
int ma[maxn];
const int inf=0x3f3f3f3f;
int root=-1;
bool okk1=false;
bool okk2=true;
void dfs(int u,int fa)
{
    if(ma[u]!=inf)
    {
        okk1=true;
        return;
    }
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(v==fa) continue;
        dfs(v,u);
    }
}

void dfsunlock(int u,int fa)
{
     ma[u]=inf;
     for(int i=0;i<g[u].size();i++)
     {
         int v=g[u][i];
         if(v==fa) continue;
         dfsunlock(v,u);
     }
}
void findf(int u)
{
    while(u!=-1)
    {
        if(ma[u]!=inf)
        {
            okk2=false;
        }
        u=pp[u];
    }
}

    LockingTree(vector<int>& p) {
        memset(ma,0x3f,sizeof(ma));
        for(int i=0;i<p.size();i++)
        {
            pp.push_back(p[i]);
        }
        for(int i=0;i<p.size();i++)
        {
            if(p[i]==-1) root=i;
            else
            {
                g[p[i]].push_back(i);
                g[i].push_back(p[i]);
            }
        }
    }

    bool lock(int num, int user) {
         if(ma[num]!=inf) return false;
         ma[num]=user;
         return true;
    }

    bool unlock(int num, int user) {
         if(ma[num]==user){
            ma[num]=inf;
            return true;
         }
         else return false;
    }

    bool upgrade(int num, int user) {
          if(ma[num]!=inf) return false;

          dfs(num,pp[num]);

          findf(pp[num]);

          if(okk1==true&&okk2==true)
          {
             dfsunlock(num,pp[num]);
             ma[num]=user;
             okk1=true;
             okk2=false;
             return true;
          }
          else{
            okk1=true;
            okk2=false;
            return false;
          }
    }


int main(void){


   	return 0;
}
